package org.itboys.mobile.controller.web.score;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.score.SundayScoreConfig;
import org.itboys.mobile.service.mysql.web.score.SundayScoreConfigService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 日期：2017年5月10日
 * 描述：圣代_积分配置_controller_web
 */
@Controller
@RequestMapping("/sunday/web/score")
public class SundayScoreConfigController extends BaseController{
    @Autowired
    private SundayScoreConfigService configService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/config/index")
    public String index(HttpServletRequest request,
                        Model model){
            return "/sunday/score/config/index";
    }


    /**
     * 查询积分配置
     * @param request
     * @param response
     */
    @RequestMapping("/config/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<SundayScoreConfig> result=configService.selectConfig(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改积分配置
     * @param request
     * @param response
     * @param config
     */
    @RequestMapping("/config/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayScoreConfig config){
        try {
            configService.saveConfig(config);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/score/config/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 获取单个积分配置
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/config/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=true) Long id){
                try{
                    SundayScoreConfig config=configService.findOne(id);
                    AjaxUtils.renderJson(response,config);
                }catch (Exception e){
                    e.printStackTrace();
                    AjaxUtils.renderJsonporJson(response, null,
                            ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
                }

    }




}
